package com.tc.report.intersect;

import java.util.ArrayList;
import java.util.List;

import com.tc.report.DnCell;
import com.tc.report.ReportAreaHandle;
import com.tc.report.ReportField;
import com.tc.report.ReportValueNote;
import com.tc.report.StringUtils;

public class RowAreaHandle extends ReportAreaHandle {
	
	public RowAreaHandle(){
		super();
	}
	public RowAreaHandle(IntersectReportImpl reportManage){
		super(reportManage);
	}
	/**
	 * 行区域的最大列跨度
	 */
	@Override
	public int getMaxColSpan() {
		// TODO Auto-generated method stub
		return 0;
	}
	/**
	 * 行区域的最大行跨度。
	 */
	@Override
	public int getMaxRowSpan() {
		// TODO Auto-generated method stub
		return 0;
	}
	/**
	 * 处理行区域的显示值清单.
	 */
	@Override
	public List<List<DnCell>> dealAreaDataValue(List<List<DnCell>> rowDataList) throws Exception {
		List<ReportValueNote> rootNoteList = this.getRootNoteList();
		if(rowDataList==null){
			rowDataList = new ArrayList<List<DnCell>>();
		}
		IntersectReportImpl reportManage = this.getReportManage();
		int titleRowNum = reportManage.getSpanRowOfRowTileCell();
		boolean isRowFieldOfValue = reportManage.isRowFieldOfValue();
		int startRow = titleRowNum;
		for(ReportValueNote curNode:rootNoteList){
			//计算当前节点的最大行跨度.
			int spanOfCurNode = curNode.getSpanOfCurNode(isRowFieldOfValue,true);
			for(int index=0;index<spanOfCurNode;index++){
				List<DnCell> rowData = new ArrayList<DnCell>();
				rowDataList.add(rowData);
			}
			this.dealAreaData(rowDataList, curNode,titleRowNum,startRow,spanOfCurNode);
			startRow = rowDataList.size();
		}
		if(reportManage.isShowRowTotal()){
			this.dealTotalRow(rowDataList, titleRowNum, startRow);
			
		}
		return rowDataList;
	}
	
	public void dealTotalRow(List<List<DnCell>> rowDataList,int titleRowNum,int startRow){
		IntersectReportImpl reportManage = this.getReportManage();
		boolean isRowFieldOfValue = reportManage.isRowFieldOfValue();
		int multiple = reportManage.getMultipleOfValArea(isRowFieldOfValue);
		int spanColOfRowArea = reportManage.getSpanColOfRowArea();
		int spancol = spanColOfRowArea;//
		if(multiple==1){//
			List<DnCell> rowData = new ArrayList<DnCell>();
			rowDataList.add(rowData);//增加一行.
			DnCell cellOfCurNode = new DnCell();
			cellOfCurNode.setFieldValue("总计");
			int spanrow = 1;//跨行数.
			cellOfCurNode.setSpanrow(spanrow);
			cellOfCurNode.setSpancol(spancol);
			int posX = 0;
			int posY = titleRowNum+startRow;//Y轴坐标需要加上标题行的数量.
			cellOfCurNode.setPosX(posX);
			cellOfCurNode.setPosY(posY);
			rowData.add(cellOfCurNode);
		}else if(multiple>1){//如果行区域需要显示值区域字段内容.
			ReportAreaHandle valAreaHandle = reportManage.getValAreaHandle();
			ReportField rootField = valAreaHandle.getRootField();
			//循环处理值区域的字段对象.
			this.dealValAreaFieldOfTotal(rowDataList, rootField, titleRowNum,spancol);
		}
	}
	
	/**
	 * 默认处理值区域字段.
	 * @param rowDataList  二维结果集.
	 * @param valField     值区域字段对象.
	 * @param posX         单元格起始X轴坐标.
	 * @param titleRowNum  标题行数.
	 * @param startRow     当前节点的起始处理行.
	 * @param prefix       值前缀.
	 */
	private void dealValAreaFieldOfTotal(List<List<DnCell>> rowDataList,ReportField valField,int titleRowNum,int spancol){
		List<DnCell> rowData = new ArrayList<DnCell>();
		rowDataList.add(rowData);//增加一行.
		DnCell cellOfValNode = new DnCell();//跨行值和跨列值都为1,默认即可.
		int spanrow = 1;//跨行数.
		cellOfValNode.setSpanrow(spanrow);
		cellOfValNode.setSpancol(spancol);
		cellOfValNode.setFieldValue(valField.getValFieldAlias()+"汇总");
		int posX = 0;
		int posY = titleRowNum+rowDataList.size();//Y轴坐标需要加上标题行的数量.
		cellOfValNode.setPosX(posX);
		cellOfValNode.setPosY(posY);
		rowData.add(cellOfValNode);
		String nextFieldName = valField.getNextFieldName();
		if(StringUtils.isNotBlank(nextFieldName)){
			IntersectReportImpl reportManage = this.getReportManage();
			ReportAreaHandle valAreaHandle = reportManage.getValAreaHandle();
			ReportField nextValField  = valAreaHandle.getReportField(nextFieldName);
			//spancol -=1;//列数减一列.
			this.dealValAreaFieldOfTotal(rowDataList, nextValField, titleRowNum,spancol);
		}
	}
	
	
	
	/**
	 * 处理一级节点的合计值列表.
	 * @param rowDataList 二维结果集.
	 * @param curNode   当前处理节点对象.
	 * @param titleRowNum  标题行数.
	 * @param startRow  当前节点的起始处理行.
	 */
	private void dealNodeTotalRow(List<List<DnCell>> rowDataList,ReportValueNote curNode,int titleRowNum,int startRow){
		IntersectReportImpl reportManage = this.getReportManage();
		boolean isRowFieldOfValue = reportManage.isRowFieldOfValue();
		int multiple = reportManage.getMultipleOfValArea(isRowFieldOfValue);
		ReportField reportField = curNode.getReportField();
		String classifyTotalType = reportField.getClassifyTotalType();//考虑分类汇总的情况.
		int spanColOfRowArea = reportManage.getSpanColOfRowArea();
		int spancol = spanColOfRowArea - (curNode.getLevel() -1);//
		if(ReportField.CLASSIFY_TOTAL_TYPE_DEFAULT.equals(classifyTotalType)){
			if(multiple==1){//
				List<DnCell> rowData = new ArrayList<DnCell>();
				if(startRow<rowDataList.size()){//如果已存在初始化行,则
					rowData = rowDataList.get(startRow);
				}else{
					rowDataList.add(rowData);//增加一行.
				}
				DnCell cellOfCurNode = new DnCell();
				cellOfCurNode.setFieldValue(curNode.getDisValue()+reportField.getTotalFieldAlias());
				int spanrow = 1;//跨行数.
				cellOfCurNode.setSpanrow(spanrow);
				cellOfCurNode.setSpancol(spancol);
				int posX = curNode.getLevel() -1;
				int posY = titleRowNum+startRow;//Y轴坐标需要加上标题行的数量.
				cellOfCurNode.setPosX(posX);
				cellOfCurNode.setPosY(posY);
				rowData.add(cellOfCurNode);
			}else if(multiple>1){//如果行区域需要显示值区域字段内容.
				ReportAreaHandle valAreaHandle = reportManage.getValAreaHandle();
				ReportField rootField = valAreaHandle.getRootField();
				//循环处理值区域的字段对象.
				this.dealValAreaFieldOfNodeTotal(rowDataList, rootField, curNode, titleRowNum, startRow,spancol);
			}
		}else if(ReportField.CLASSIFY_TOTAL_TYPE_DEFINDE.equals(classifyTotalType)){
			//TODO
		}
	}
	
	
	/**
	 * 默认处理值区域字段.
	 * @param rowDataList  二维结果集.
	 * @param valField     值区域字段对象.
	 * @param posX         单元格起始X轴坐标.
	 * @param titleRowNum  标题行数.
	 * @param startRow     当前节点的起始处理行.
	 * @param prefix       值前缀.
	 */
	private void dealValAreaFieldOfNodeTotal(List<List<DnCell>> rowDataList,ReportField valField,ReportValueNote curNode,int titleRowNum,int startRow,int spancol){
		List<DnCell> rowData = new ArrayList<DnCell>();
		if(startRow<rowDataList.size()){//如果已存在初始化行,则
			rowData = rowDataList.get(startRow);
		}else{
			rowDataList.add(rowData);//增加一行.
		}
		DnCell cellOfValNode = new DnCell();//跨行值和跨列值都为1,默认即可.
		int spanrow = 1;//跨行数.
		cellOfValNode.setSpanrow(spanrow);
		cellOfValNode.setSpancol(spancol);
		cellOfValNode.setFieldValue(curNode.getDisValue()+valField.getValFieldAlias());
		int posX = curNode.getLevel() -1;
		int posY = titleRowNum+startRow;//Y轴坐标需要加上标题行的数量.
		cellOfValNode.setPosX(posX);
		cellOfValNode.setPosY(posY);
		rowData.add(cellOfValNode);
		String nextFieldName = valField.getNextFieldName();
		if(StringUtils.isNotBlank(nextFieldName)){
			IntersectReportImpl reportManage = this.getReportManage();
			ReportAreaHandle valAreaHandle = reportManage.getValAreaHandle();
			ReportField nextValField  = valAreaHandle.getReportField(nextFieldName);
			startRow+=1;//下一个字段起始位置需要增加一行.
			//spancol -=1;//列数减一列.
			this.dealValAreaFieldOfNodeTotal(rowDataList, nextValField, curNode, titleRowNum, startRow,spancol);
		}
	}
	
	
	
	/**
	 * 
	 * @param rowDataList  二维结果集.
	 * @param curNode      当前处理节点对象.
	 * @param titleRowNum  标题行数.
	 * @param startRow     当前节点的起始处理行.
	 * @param spanOfCurNode 当前节点的跨行数.
	 * @throws Exception
	 */
	private int dealAreaData(List<List<DnCell>> rowDataList,ReportValueNote curNode,int titleRowNum,int startRow,int spanOfCurNode)throws Exception{
		IntersectReportImpl reportManage = this.getReportManage();
		List<ReportValueNote> sonList = curNode.getSonList();
		int curRowNum = startRow;//当前行数.
		List<DnCell> rowData = rowDataList.get(curRowNum);
		DnCell cellOfCurNode = new DnCell();
		cellOfCurNode.setFieldValue(curNode.getDisValue());
		int spancol = 1;//跨列数.
		int spanrow = spanOfCurNode;//跨行数.
		cellOfCurNode.setSpanrow(spanrow);
		cellOfCurNode.setSpancol(spancol);
		int posX = curNode.getLevel()-1;//X轴坐标与当前节点的层次一致.层次从1开始.坐标从0开始.
		int posY = titleRowNum + startRow;//Y轴坐标需要加上标题行的数量.
		cellOfCurNode.setPosX(posX);
		cellOfCurNode.setPosY(posY);
		rowData.add(cellOfCurNode);
		//判断值区域是否存在多个字段,并判断该值区域的标题单元是否需要显示在行区域中.
		int multiple = reportManage.getMultipleOfValArea(reportManage.isRowFieldOfValue());
		if(sonList.size()==0){//如果不存在子节点.最后一个字段节点,
			if(multiple>1){//如果大于1,说明存在多个值字段,并且在行区域中显示.
				ReportAreaHandle valAreaHandle = reportManage.getValAreaHandle();
				ReportField rootField = valAreaHandle.getRootField();
				int valPosX = posX+1;
				this.dealValAreaField(rowDataList, rootField, valPosX, titleRowNum, startRow);
			}
		}
		boolean isRowFieldOfValue = reportManage.isRowFieldOfValue();
		//再递归处理子节点信息.
		for(ReportValueNote sonNode:sonList){
			int spanOfSonNode = sonNode.getSpanOfCurNode(isRowFieldOfValue,true);
			curRowNum =this.dealAreaData(rowDataList, sonNode,titleRowNum,curRowNum,spanOfSonNode);
			curRowNum +=multiple;//设置下一个子节点的起始行.
		}
		//处理当前节点的子节点.
		if(curNode.isShowTotalCell()){//处理合计行的值.
			this.dealNodeTotalRow(rowDataList, curNode, titleRowNum, curRowNum);
		}
		return curRowNum;
	}
	
	/**
	 * 默认处理值区域字段.
	 * @param rowDataList  二维结果集.
	 * @param valField     值区域字段对象.
	 * @param posX         单元格起始X轴坐标.
	 * @param titleRowNum  标题行数.
	 * @param startRow     当前节点的起始处理行.
	 * @param prefix       值前缀.
	 */
	private void dealValAreaField(List<List<DnCell>> rowDataList,ReportField valField,int posX,int titleRowNum,int startRow){
		DnCell cellOfValNode = new DnCell();//跨行值和跨列值都为1,默认即可.
		cellOfValNode.setFieldValue(valField.getValFieldAlias());
		int posY = titleRowNum+startRow;//Y轴坐标需要加上标题行的数量.
		cellOfValNode.setPosX(posX);
		cellOfValNode.setPosY(posY);
		List<DnCell> rowData = rowDataList.get(startRow);
		rowData.add(cellOfValNode);
		String nextFieldName = valField.getNextFieldName();
		if(StringUtils.isNotBlank(nextFieldName)){
			IntersectReportImpl reportManage = this.getReportManage();
			ReportAreaHandle valAreaHandle = reportManage.getValAreaHandle();
			ReportField nextValField  = valAreaHandle.getReportField(nextFieldName);
			startRow+=1;//下一个字段起始位置需要增加一行.
			this.dealValAreaField(rowDataList, nextValField, posX, titleRowNum, startRow);
		}
	}
	
	
	
	
}
